import java.util.ArrayList;
import java.util.Scanner;
import static java.lang.System.out;
public class Entrance {
    public static void main(String[] args) {
        int id;
        Scanner input = new Scanner(System.in);
        FilmController filmController = new FilmController();
        UserController userController = new UserController();
        while (true) {
            out.println("* -------------------电影信息管理系统-------------------- *");
            out.println("* 操作编号：\t\t\t\t\t\t\t\t\t\t\t\t*");
            out.println("* \t0.退出系统\t\t\t\t\t\t\t\t\t\t\t*");
            out.println("* \t1.登录\t\t\t\t\t\t\t\t\t\t\t\t*");
            out.println("* \t2.添加一部电影\t\t\t\t\t\t\t\t\t\t*");
            out.println("* \t3.查询所有电影信息\t\t\t\t\t\t\t\t\t*");
            out.println("* \t4.根据编号查询电影信息\t\t\t\t\t\t\t\t*");
            out.println("* \t5.根据名称查询电影信息\t\t\t\t\t\t\t\t*");
            out.println("* \t6.删除一条电影信息\t\t\t\t\t\t\t\t\t*");
            out.println("* \t7.修改一条电影信息\t\t\t\t\t\t\t\t\t*");
            out.println("* \t8.添加一名用户\t\t\t\t\t\t\t\t\t\t*");
            out.println("* \t9.查询所有用户信息\t\t\t\t\t\t\t\t\t*");
            out.println("* \t10.根据姓名查询用户信息\t\t\t\t\t\t\t\t*");
            out.println("* \t11.根据编号查询用户信息\t\t\t\t\t\t\t\t*");
            out.println("* \t12.删除一条用户信息\t\t\t\t\t\t\t\t\t*");
            out.println("* \t13.修改一条用户信息\t\t\t\t\t\t\t\t\t*");
            out.println("* ----------------------------------------------------- *");
            out.print("请输入：");
            id = input.nextInt();

            switch (id) {
                case 0:
                    System.exit(0);
                    break;
                case 1:
                    out.print("请输入用户编号: ");
                    int u_id = input.nextInt();
                    out.print("\n请输入用户名字: ");
                    String u_name = input.next();
                    out.print("\n请输入用户密码：");
                    String u_password = input.next();
                    out.println("\n请输入用户角色 (admin, user)：");
                    String u_role = input.next();
                    userController.login(new User(u_id, u_name, u_password, u_role));
                    break;
                case 2:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    out.print("请输入电影编号: ");
                    int f_id = input.nextInt();
                    out.print("\n请输入电影名称: ");
                    String f_name = input.next();
                    out.print("\n请输入电影上映时间(yyyy-MM-dd)：");
                    String f_release_date = input.next();
                    out.print("\n请输入电影时长(HH:mm:ss)：");
                    String f_duration = input.next();
                    filmController.insert(new Film(f_id, f_name, f_release_date, f_duration),
                            filmController.getFilms());
                    break;
                case 3:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    ArrayList<Film> films = filmController.selectAll();
                    out.printf("%1$-5s%2$-10s%3$-15s%4$-10s%n", "编号", "名称", "上映时间", "时长");
                    films.forEach((e) -> out.println(e.toString()));
                    break;
                case 4:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    out.print("请输入电影编号：");
                    Integer f_id2 = input.nextInt();
                    ArrayList<Film> films1 = filmController.selectById(f_id2, filmController.getFilms());
                    films1.forEach((e) -> System.out.println(e.toString()));
                    break;
                case 5:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    out.print("请输入电影名称：");
                    String f_name2 = input.next();
                    ArrayList<Film> films2 = filmController.selectByName(f_name2, filmController.getFilms());
                    films2.forEach((e) -> System.out.println(e.toString()));
                    break;
                case 6:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    out.print("请输入要删除的电影编号：");
                    int f_id3 = input.nextInt();
                    filmController.delete(f_id3, filmController.getFilms());
                    break;
                case 7:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    out.print("请输入电影编号: ");
                    int f_id4 = input.nextInt();
                    out.print("\n请输入电影名称: ");
                    String f_name4 = input.next();
                    out.print("\n请输入电影上映时间(yyyy-MM-dd)：");
                    String f_release_date4 = input.next();
                    out.print("\n请输入电影时长(HH:mm:ss)：");
                    String f_duration4 = input.next();
                    filmController.update(new Film(f_id4, f_name4, f_release_date4, f_duration4),
                            filmController.getFilms());
                    break;
                case 8:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    if(userController.validate_auth()) { // 验证user角色
                        out.print("请输入用户编号: ");
                        int u_id2 = input.nextInt();
                        out.print("\n请输入用户名: ");
                        String u_name2 = input.next();
                        out.print("\n请输入用户密码：");
                        String u_password2 = input.next();
                        out.print("\n请输入用户角色 (admin, user)：");
                        String u_role2 = input.next();
                        userController.insert(new User(u_id2, u_name2, u_password2, u_role2),
                                userController.getUsers());
                    }else out.println("您没有此权限！");
                    break;
                case 9:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    if(userController.validate_auth()) { // 验证user角色
                        ArrayList<User> userArrayList = userController.selectAll();
                        out.printf("%1$-5s%2$-10s%3$-15s%4$-10s%n", "编号", "用户名", "密码", "角色");
                        userArrayList.forEach((e) -> out.println(e.toString()));
                    }else out.println("您没有此权限！");
                    break;
                case 10:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    if(userController.validate_auth()) { // 验证user角色
                        out.print("请输入用户名：");
                        String u_name3 = input.next();
                        ArrayList<User> userArrayList2 = userController.selectByName(u_name3,
                                userController.getUsers());
                        userArrayList2.forEach((e) -> System.out.println(e.toString()));
                    }else out.println("您没有此权限！");
                    break;
                case 11:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    if(userController.validate_auth()) { // 验证user角色
                        out.print("请输入用户编号：");
                        Integer u_id3 = input.nextInt();
                        ArrayList<User> userArrayList3 = userController.selectById(u_id3,
                                userController.getUsers());
                        userArrayList3.forEach((e) -> System.out.println(e.toString()));
                    }else out.println("您没有此权限！");
                    break;
                case 12:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    if(userController.validate_auth()) { // 验证user角色
                        out.print("请输入要删除的用户编号：");
                        int u_id4 = input.nextInt();
                        userController.delete(u_id4, userController.getUsers());
                    }else out.println("您没有此权限！");
                    break;
                case 13:
                    if (!userController.is_login()) {
                        out.println("请先登录！");
                        break;
                    }
                    if(userController.validate_auth()) { // 验证user角色
                        out.print("请输入用户编号: ");
                        int u_id5 = input.nextInt();
                        out.print("\n请输入用户名: ");
                        String u_name5 = input.next();
                        out.print("\n请输入用户密码：");
                        String u_password5 = input.next();
                        out.print("\n请输入用户角色 (admin, user)：");
                        String u_role5 = input.next();
                        userController.update(new User(u_id5, u_name5, u_password5, u_role5),
                                userController.getUsers());
                    }else out.println("您没有此权限！");
                    break;
            }
        }
    }
}
